//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.IO;
using System.Net;
using System.Web;   

namespace CommunityServer.Components {
    
    public class PostAttachment : PostAttachmentMetaData
    {
        public PostAttachment() 
        {
        }

        Stream content = null;

		public PostAttachment(string _fileName, string _contentType, Stream stream)
		{
			this.Length = stream.Length;
			this.ContentType = _contentType;
			this.FileName = _fileName;
			this.Content = stream;
			CheckAndProcessImage(this.Content);
		}

        public PostAttachment(string httpFileName)
        {
			try
			{
				using(HttpWebResponse response =  CSRequest.GetResponse(httpFileName))
				{
					this.Length =  (int)response.ContentLength;
					this.ContentType = response.ContentType ;
					this.FileName = httpFileName;
					this.Height = 0;
					this.Width = 0;
					response.Close();
				}
			}
			catch 
			{
				this.Length = -1;
				this.FileName = httpFileName;
				this.ContentType = Configuration.AttachmentSettings.GetMapping(this.FileName);
				this.Height = 0;
				this.Width = 0;
			}
        }
		public PostAttachment(PostAttachment postAttachment) : this((PostAttachmentMetaData)postAttachment)
		{
			this.Content = postAttachment.Content;
		}
    	
    	public PostAttachment(PostAttachmentMetaData metadata)
    	{
			this.Length = metadata.Length;
			this.ContentType = metadata.ContentType;
			this.FileName = metadata.FileName;
			this.Height = metadata.Height;
			this.Width = metadata.Width;
			this.FriendlyFileName = metadata.FriendlyFileName;
			this.IsRemote = metadata.IsRemote;
			this.PostID = metadata.PostID;
			this.UserID = metadata.UserID;
			if (metadata.HasDateCreated)
				this.DateCreated = metadata.DateCreated;
		}

        public PostAttachment (HttpPostedFile postedFile) 
        {

            // Get the file length and content type
            //
            this.Length = postedFile.ContentLength;
            this.ContentType = postedFile.ContentType;

            // Get the filename
            //
            this.FileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("\\") + 1);

            // Setup the byte array
            //
            byte[] data = new byte[this.Length];

            // Read in the attachment into a byte array
            //
            postedFile.InputStream.Read(data, 0, (int)Length);
            Content = new MemoryStream(data);

			CheckAndProcessImage(postedFile.InputStream);

        }

		protected void CheckAndProcessImage(Stream stream)
		{
			if(IsImage)
			{
				try
				{
					ImageInfo info = new ImageInfo(stream);
					info.Check();
					this.Width = info.Width;
					this.Height = info.Height;
				}
				catch{}
			}
			
		}

        public Stream Content 
        {
            get 
            {
                return content;
            }
            set 
            {
                content = value;
            }
        }
    }
    
    /// <summary>
    /// Summary description for PostAttachment.
    /// </summary>
    public class PostAttachmentMetaData {
		long length = 0;
		int height = 0;
		int width = 0;
		int userID = 0;
        int postID = 0;
        int sectionID = 0;
        DateTime created;
        string contentType;
        string fileName;
        string friendlyfileName;
        
        bool isRemote;

        public PostAttachmentMetaData() {}
   

        public long Length {
            get {
                return length;
            }
            set {
                length = value;
            }
        }

        public string ContentType {
            get {
                return contentType;
            }
            set {
                contentType = value.Replace("pjpeg", "jpeg");
            }
        }

        public string FileName {
            get {
                return fileName;
            }
            set {
                fileName = value;
            }
        }

        public string FriendlyFileName 
        {
            get 
            {
                if(friendlyfileName == null)
                    return FileName;

                return friendlyfileName;
            }
            set 
            {
                friendlyfileName = value;
            }
        }
        

        public bool IsRemote 
        {
            get 
            {
                return isRemote;
            }
            set 
            {
                isRemote = value;
            }
        }



        public int PostID {
            get { return postID; }
            set { postID = value; }
        }

        public int SectionID {
            get { return sectionID; }
            set { sectionID = value; }
        }

        public int UserID {
            get { return userID; }
            set { userID = value; }
        }

        public DateTime DateCreated {
            get {
                return created;
            }
            set {
                created = value;
            }
        }

		public bool IsImage
		{
			get
			{
				if(ContentType != null)
					return (ContentType.IndexOf("image") > -1);
				else
					return false;
			}
		}

    	public int Height
    	{
			get { return height; }
			set { height = value; }
		}

    	public int Width
    	{
			get { return width; }
			set { width = value; }
		}

		public bool HasDateCreated
		{
			get
			{
				try
				{
					return (DateCreated.Year > 0);
				}
				catch
				{
					return false;
				}
			}
		}

    }
}
